<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" integrity="sha256-Z1K5uhUaJXA7Ll0XrZ/0JhX4lAtZFpT6jkKrEDT0drU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.14.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":-1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="数据库查看1show dbs  创建1use data_base_name  删除1234# 先切过去use to_delete_db# 后删除db.dropDatabase()  集合查看1db.getCollectionNames()  创建1234# 创建集合db.createCollection(collection_name,options)# 或者直接使用集合名去插入文档，就自动创建集">
<meta property="og:type" content="article">
<meta property="og:title" content="超详细的MongoDB的增删改查学习笔记！">
<meta property="og:url" content="http://example.com/2021/10/04/%E8%B6%85%E8%AF%A6%E7%BB%86%E7%9A%84MongoDB%E7%9A%84%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%EF%BC%81/index.html">
<meta property="og:site_name" content="JsyBlog">
<meta property="og:description" content="数据库查看1show dbs  创建1use data_base_name  删除1234# 先切过去use to_delete_db# 后删除db.dropDatabase()  集合查看1db.getCollectionNames()  创建1234# 创建集合db.createCollection(collection_name,options)# 或者直接使用集合名去插入文档，就自动创建集">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-10-04T08:34:47.000Z">
<meta property="article:modified_time" content="2021-10-22T02:45:54.828Z">
<meta property="article:author" content="SongyangJi">
<meta property="article:tag" content="NoSQL">
<meta property="article:tag" content="MongoDB">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2021/10/04/%E8%B6%85%E8%AF%A6%E7%BB%86%E7%9A%84MongoDB%E7%9A%84%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%EF%BC%81/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2021/10/04/%E8%B6%85%E8%AF%A6%E7%BB%86%E7%9A%84MongoDB%E7%9A%84%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%EF%BC%81/","path":"2021/10/04/超详细的MongoDB的增删改查学习笔记！/","title":"超详细的MongoDB的增删改查学习笔记！"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>超详细的MongoDB的增删改查学习笔记！ | JsyBlog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">JsyBlog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="nav-number">1.</span> <span class="nav-text">数据库</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9F%A5%E7%9C%8B"><span class="nav-number">1.1.</span> <span class="nav-text">查看</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%9B%E5%BB%BA"><span class="nav-number">1.2.</span> <span class="nav-text">创建</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%A0%E9%99%A4"><span class="nav-number">1.3.</span> <span class="nav-text">删除</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E9%9B%86%E5%90%88"><span class="nav-number">2.</span> <span class="nav-text">集合</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9F%A5%E7%9C%8B-1"><span class="nav-number">2.1.</span> <span class="nav-text">查看</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%9B%E5%BB%BA-1"><span class="nav-number">2.2.</span> <span class="nav-text">创建</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%A0%E9%99%A4-1"><span class="nav-number">2.3.</span> <span class="nav-text">删除</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%96%87%E6%A1%A3"><span class="nav-number">3.</span> <span class="nav-text">文档</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8F%92%E5%85%A5%E6%96%87%E6%A1%A3"><span class="nav-number">3.1.</span> <span class="nav-text">插入文档</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#API"><span class="nav-number">3.1.1.</span> <span class="nav-text">API</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8F%92%E5%85%A5%E8%A1%8C%E4%B8%BA"><span class="nav-number">3.1.2.</span> <span class="nav-text">插入行为</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9F%A5%E8%AF%A2%E6%96%87%E6%A1%A3"><span class="nav-number">3.2.</span> <span class="nav-text">查询文档</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="nav-number">3.2.1.</span> <span class="nav-text">过滤器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">3.2.2.</span> <span class="nav-text">运算符</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%AF%94%E8%BE%83%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">3.2.2.1.</span> <span class="nav-text">比较运算符</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">3.2.2.2.</span> <span class="nav-text">逻辑运算符</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%85%B6%E4%BB%96%E6%9F%A5%E8%AF%A2%E6%8A%80%E5%B7%A7%E3%80%81%E7%94%A8%E6%B3%95"><span class="nav-number">3.2.3.</span> <span class="nav-text">其他查询技巧、用法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%B5%8C%E5%A5%97%E6%96%87%E6%A1%A3"><span class="nav-number">3.2.3.1.</span> <span class="nav-text">嵌套文档</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%B5%8C%E5%A5%97%E5%AD%97%E6%AE%B5"><span class="nav-number">3.2.3.2.</span> <span class="nav-text">嵌套字段</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%85%B3%E4%BA%8E%E6%95%B0%E7%BB%84"><span class="nav-number">3.2.4.</span> <span class="nav-text">关于数组</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%95%B0%E7%BB%84%E7%9B%B8%E5%85%B3%E8%BF%90%E7%AE%97%E7%AC%A6%E3%80%82"><span class="nav-number">3.2.4.1.</span> <span class="nav-text">数组相关运算符。</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%85%B3%E4%BA%8E%E5%B5%8C%E5%85%A5%E6%96%87%E6%A1%A3%E7%9A%84%E6%95%B0%E7%BB%84"><span class="nav-number">3.2.5.</span> <span class="nav-text">关于嵌入文档的数组</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9F%A5%E8%AF%A2%E5%B5%8C%E5%A5%97%E5%9C%A8%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E6%96%87%E6%A1%A3"><span class="nav-number">3.2.5.1.</span> <span class="nav-text">查询嵌套在数组中的文档</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%9C%A8%E6%96%87%E6%A1%A3%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E5%AD%97%E6%AE%B5%E4%B8%8A%E6%8C%87%E5%AE%9A%E6%9F%A5%E8%AF%A2%E6%9D%A1%E4%BB%B6"><span class="nav-number">3.2.5.2.</span> <span class="nav-text">在文档数组中的字段上指定查询条件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%B8%BA%E6%96%87%E6%A1%A3%E6%95%B0%E7%BB%84%E6%8C%87%E5%AE%9A%E5%A4%9A%E4%B8%AA%E6%9D%A1%E4%BB%B6"><span class="nav-number">3.2.5.3.</span> <span class="nav-text">为文档数组指定多个条件</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BF%94%E5%9B%9E%E6%96%87%E6%A1%A3%E7%9A%84%E9%83%A8%E5%88%86%E5%AD%97%E6%AE%B5"><span class="nav-number">3.2.6.</span> <span class="nav-text">返回文档的部分字段</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9F%A5%E8%AF%A2%E7%A9%BA%E6%88%96%E7%BC%BA%E5%A4%B1%E5%AD%97%E6%AE%B5"><span class="nav-number">3.2.7.</span> <span class="nav-text">查询空或缺失字段</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%9B%B8%E7%AD%89%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="nav-number">3.2.7.1.</span> <span class="nav-text">相等过滤器</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%B1%BB%E5%9E%8B%E6%A3%80%E6%9F%A5"><span class="nav-number">3.2.7.2.</span> <span class="nav-text">类型检查</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AD%98%E5%9C%A8%E6%80%A7%E6%A3%80%E6%9F%A5"><span class="nav-number">3.2.7.3.</span> <span class="nav-text">存在性检查</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9B%B4%E6%96%B0%E6%96%87%E6%A1%A3"><span class="nav-number">3.3.</span> <span class="nav-text">更新文档</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9B%B4%E6%96%B0%E6%96%B9%E6%B3%95"><span class="nav-number">3.3.1.</span> <span class="nav-text">更新方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9B%B4%E6%96%B0%E6%93%8D%E4%BD%9C"><span class="nav-number">3.3.2.</span> <span class="nav-text">更新操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9B%B4%E6%96%B0%E6%93%8D%E4%BD%9C%E7%AC%A6"><span class="nav-number">3.3.3.</span> <span class="nav-text">更新操作符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%97%E6%AE%B5"><span class="nav-number">3.3.4.</span> <span class="nav-text">字段</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%B0%E7%BB%84"><span class="nav-number">3.3.5.</span> <span class="nav-text">数组</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6-1"><span class="nav-number">3.3.5.1.</span> <span class="nav-text">运算符</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BF%AE%E9%A5%B0%E7%AC%A6"><span class="nav-number">3.3.5.2.</span> <span class="nav-text">修饰符</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8C%89%E4%BD%8D"><span class="nav-number">3.3.6.</span> <span class="nav-text">按位</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%A1%8C%E4%B8%BA"><span class="nav-number">3.3.7.</span> <span class="nav-text">行为</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%A0%E9%99%A4%E6%96%87%E6%A1%A3"><span class="nav-number">3.4.</span> <span class="nav-text">删除文档</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%A0%E9%99%A4%E6%96%B9%E6%B3%95"><span class="nav-number">3.4.1.</span> <span class="nav-text">删除方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%A1%8C%E4%B8%BA-1"><span class="nav-number">3.4.2.</span> <span class="nav-text">行为</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">SongyangJi</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">251</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">45</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">109</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/10/04/%E8%B6%85%E8%AF%A6%E7%BB%86%E7%9A%84MongoDB%E7%9A%84%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%EF%BC%81/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="超详细的MongoDB的增删改查学习笔记！ | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          超详细的MongoDB的增删改查学习笔记！
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-10-04 16:34:47" itemprop="dateCreated datePublished" datetime="2021-10-04T16:34:47+08:00">2021-10-04</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2021-10-22 10:45:54" itemprop="dateModified" datetime="2021-10-22T10:45:54+08:00">2021-10-22</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/NoSQL/" itemprop="url" rel="index"><span itemprop="name">NoSQL</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="数据库"><a href="#数据库" class="headerlink" title="数据库"></a>数据库</h1><h2 id="查看"><a href="#查看" class="headerlink" title="查看"></a>查看</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">show dbs</span><br></pre></td></tr></table></figure>

<h2 id="创建"><a href="#创建" class="headerlink" title="创建"></a>创建</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">use data_base_name</span><br></pre></td></tr></table></figure>

<h2 id="删除"><a href="#删除" class="headerlink" title="删除"></a>删除</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 先切过去</span></span><br><span class="line">use to_delete_db</span><br><span class="line"><span class="comment"># 后删除</span></span><br><span class="line">db.dropDatabase()</span><br></pre></td></tr></table></figure>

<h1 id="集合"><a href="#集合" class="headerlink" title="集合"></a>集合</h1><h2 id="查看-1"><a href="#查看-1" class="headerlink" title="查看"></a>查看</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.getCollectionNames()</span><br></pre></td></tr></table></figure>

<h2 id="创建-1"><a href="#创建-1" class="headerlink" title="创建"></a>创建</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建集合</span></span><br><span class="line">db.createCollection(collection_name,options)</span><br><span class="line"><span class="comment"># 或者直接使用集合名去插入文档，就自动创建集合</span></span><br><span class="line">db.mycol.insert(&#123;<span class="string">&quot;key&quot;</span> : <span class="string">&quot;value&quot;</span>&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="删除-1"><a href="#删除-1" class="headerlink" title="删除"></a>删除</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.collection_name.drop()</span><br></pre></td></tr></table></figure>


<h1 id="文档"><a href="#文档" class="headerlink" title="文档"></a>文档</h1><h2 id="插入文档"><a href="#插入文档" class="headerlink" title="插入文档"></a>插入文档</h2><h3 id="API"><a href="#API" class="headerlink" title="API"></a>API</h3><ul>
<li><code>save()</code>：如果 _id 主键存在则更新数据，如果不存在就插入数据。该方法新版本中已废弃，可以使用 db.collection.insertOne() 或 db.collection.replaceOne() 来代替。</li>
<li><code>insert()</code>: 若插入的数据主键已经存在，则会抛 org.springframework.dao.DuplicateKeyException 异常，提示主键重复，不保存当前数据。</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">db.COLLECTION_NAME.insert(document)</span><br><span class="line"></span><br><span class="line">db.COLLECTION_NAME.save(document)</span><br></pre></td></tr></table></figure>

<p><strong>不过下面的两个方法都已经过期，不要使用了.</strong></p>
<ul>
<li><p><code>insertOne()</code><br>传入一个json对象。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.insertOne(</span><br><span class="line">   &#123; item: <span class="string">&quot;canvas&quot;</span>, qty: 100, tags: [<span class="string">&quot;cotton&quot;</span>], size: &#123; h: 28, w: 35.5, uom: <span class="string">&quot;cm&quot;</span> &#125; &#125;</span><br><span class="line">)</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>insertMany()</code><br>传入一个数组，里面是Json对象。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.insertMany([</span><br><span class="line">   &#123; item: <span class="string">&quot;journal&quot;</span>, qty: 25, tags: [<span class="string">&quot;blank&quot;</span>, <span class="string">&quot;red&quot;</span>], size: &#123; h: 14, w: 21, uom: <span class="string">&quot;cm&quot;</span> &#125; &#125;,</span><br><span class="line">   &#123; item: <span class="string">&quot;mat&quot;</span>, qty: 85, tags: [<span class="string">&quot;gray&quot;</span>], size: &#123; h: 27.9, w: 35.5, uom: <span class="string">&quot;cm&quot;</span> &#125; &#125;,</span><br><span class="line">   &#123; item: <span class="string">&quot;mousepad&quot;</span>, qty: 25, tags: [<span class="string">&quot;gel&quot;</span>, <span class="string">&quot;blue&quot;</span>], size: &#123; h: 19, w: 22.85, uom: <span class="string">&quot;cm&quot;</span> &#125; &#125;</span><br><span class="line">])</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="插入行为"><a href="#插入行为" class="headerlink" title="插入行为"></a>插入行为</h3><ul>
<li><p>集合创建<br>如果该集合当前不存在，则插入操作将创建该集合。</p>
</li>
<li><p>_id字段<br>在MongoDB中，存储在集合中的每个文档都需要一个唯一的 _id字段作为主键。如果插入的文档省略了该_id字段，则MongoDB驱动程序会自动为该字段生成一个ObjectId, 键名为”_id” 。</p>
</li>
<li><p>原子性<br>MongoDB中的所有写操作在单个文档级别上都是原子性的。</p>
</li>
<li><p>写确认<br>对于写入问题，您可以指定从MongoDB请求的写入操作的确认级别。</p>
</li>
</ul>
<h2 id="查询文档"><a href="#查询文档" class="headerlink" title="查询文档"></a>查询文档</h2><p>在任何数据库中，差不多查询是花样的最多的了。</p>
<h3 id="过滤器"><a href="#过滤器" class="headerlink" title="过滤器"></a>过滤器</h3><p>先介绍一个所谓的过滤器概念。<br>这个也就是<strong>对匹配的文档进行操作</strong>，如查询、投影、更改、删除。</p>
<p>在MongoDB里，文档是一个重要的要素，<strong>过滤器也是一个文档</strong>。<br><strong>文档通过并列、嵌套形成更复杂的查询条件。</strong></p>
<p>也不奇怪，MongoDB被称为文档型数据库了。</p>
<p>最简单的过滤器就是<strong>空过滤器</strong>。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 空文档</span></span><br><span class="line">db.inventory.find( &#123;&#125; )</span><br><span class="line"><span class="comment"># </span></span><br><span class="line">db.inventory.find()</span><br></pre></td></tr></table></figure>

<h3 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h3><p><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/query/#std-label-query-selectors">更多的运算符，看这里</a></p>
<h4 id="比较运算符"><a href="#比较运算符" class="headerlink" title="比较运算符"></a>比较运算符</h4><table>
<thead>
<tr>
<th>运算符</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>$eq</td>
<td>匹配等于指定值的值。 （&#x3D;）</td>
</tr>
<tr>
<td>$gt</td>
<td>匹配大于指定值的值。（&gt;）</td>
</tr>
<tr>
<td>$gte</td>
<td>匹配大于或等于指定值的值。（&gt;&#x3D;）</td>
</tr>
<tr>
<td>$in</td>
<td>匹配数组中指定的任何值。</td>
</tr>
<tr>
<td>$lt</td>
<td>匹配小于指定值的值。（&lt;）</td>
</tr>
<tr>
<td>$lte</td>
<td>匹配小于或等于指定值的值。（&lt;&#x3D;）</td>
</tr>
<tr>
<td>$ne</td>
<td>匹配所有不等于指定值的值。 （!&#x3D;）</td>
</tr>
<tr>
<td>$nin</td>
<td>不匹配数组中指定的任何值。</td>
</tr>
</tbody></table>
<p>举例：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 相等条件</span></span><br><span class="line">&#123; &lt;field1&gt;: &lt;value1&gt;, ... &#125; </span><br><span class="line"><span class="comment"># 不等于条件</span></span><br><span class="line">&#123;&lt;key&gt;:&#123;<span class="string">&quot;<span class="variable">$ne</span>&quot;</span>:&lt;value&gt;&#125;&#125;</span><br><span class="line"><span class="comment"># 大于</span></span><br><span class="line">&#123;&lt;key&gt;:&#123;<span class="string">&quot;<span class="variable">$gt</span>&quot;</span>:&lt;value&gt;&#125;&#125;</span><br><span class="line"><span class="comment"># 小于</span></span><br><span class="line">&#123;&lt;key&gt;:&#123;<span class="string">&quot;<span class="variable">$lt</span>&quot;</span>:&lt;value&gt;&#125;&#125;</span><br><span class="line"><span class="comment"># 小于等于</span></span><br><span class="line">&#123;&lt;key&gt;:&#123;<span class="string">&quot;<span class="variable">$gte</span>&quot;</span>:&lt;value&gt;&#125;&#125;</span><br><span class="line"><span class="comment"># 大于等于</span></span><br><span class="line">&#123;&lt;key&gt;:&#123;<span class="string">&quot;<span class="variable">$lte</span>&quot;</span>:&lt;value&gt;&#125;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># in</span></span><br><span class="line">&#123;&lt;key&gt;:&#123;<span class="string">&quot;<span class="variable">$in</span>&quot;</span>:[&lt;v1&gt;,&lt;v2&gt;,...]&#125;</span><br></pre></td></tr></table></figure>


<h4 id="逻辑运算符"><a href="#逻辑运算符" class="headerlink" title="逻辑运算符"></a>逻辑运算符</h4><table>
<thead>
<tr>
<th>运算符</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>$and</td>
<td>用逻辑联接查询子句AND将返回两个子句都符合条件的所有文档。</td>
</tr>
<tr>
<td>$not</td>
<td>反转查询表达式的效果，并返回与查询表达式不匹配的文档。</td>
</tr>
<tr>
<td>$nor</td>
<td>用逻辑联接查询子句NOR将返回两个子句均不匹配的所有文档。</td>
</tr>
<tr>
<td>$or</td>
<td>用逻辑联接查询子句OR将返回符合任一子句条件的所有文档。</td>
</tr>
</tbody></table>
<ul>
<li><strong>指定OR条件</strong><br>使用$or运算符，您可以指定一个复合查询，该查询将每个子句与逻辑连接符连接在一起，OR以便该查询选择集合中至少匹配一个条件的文档。</li>
</ul>
<p><strong>注意它的组织方式，其中键是”$or” ；值是一个数组，数组里面是 若干个文档，当然还可以继续嵌套下去。</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123; <span class="variable">$or</span>: [ &#123; status: <span class="string">&quot;A&quot;</span> &#125;, &#123; qty: &#123; <span class="variable">$lt</span>: 30 &#125; &#125; ] &#125; )</span><br></pre></td></tr></table></figure>

<ul>
<li><strong>指定AND条件</strong><br>对于AND的话，当然也可以按照OR的方式依葫芦画瓢。不过，还可以直接隐式使用 <code>,</code>分割多个键值对。<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123; status: <span class="string">&quot;A&quot;</span>, qty: &#123; <span class="variable">$lt</span>: 30 &#125; &#125; )</span><br></pre></td></tr></table></figure></li>
</ul>
<p>同时指定AND和OR</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123;</span><br><span class="line">     status: <span class="string">&quot;A&quot;</span>,</span><br><span class="line">     <span class="variable">$or</span>: [ &#123; qty: &#123; <span class="variable">$lt</span>: 30 &#125; &#125;, &#123; item: <span class="string">&#x27;p&#x27;</span> &#125; ]</span><br><span class="line">&#125; )</span><br></pre></td></tr></table></figure>

<p>其余的运算符就不细说了，这里只介绍最常用的。</p>
<h3 id="其他查询技巧、用法"><a href="#其他查询技巧、用法" class="headerlink" title="其他查询技巧、用法"></a>其他查询技巧、用法</h3><h4 id="嵌套文档"><a href="#嵌套文档" class="headerlink" title="嵌套文档"></a>嵌套文档</h4><p>说白了，那些过滤器不仅仅可以出现数字、字符串这些，可以<strong>用文档过滤</strong>。</p>
<p>如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123; size: &#123; h: 14, w: 21, uom: <span class="string">&quot;cm&quot;</span> &#125; &#125; )</span><br></pre></td></tr></table></figure>
<h4 id="嵌套字段"><a href="#嵌套字段" class="headerlink" title="嵌套字段"></a>嵌套字段</h4><p>就像使用 对象一样，链式地选择某个字段。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123; <span class="string">&quot;size.h&quot;</span>: &#123; <span class="variable">$lt</span>: 15 &#125; &#125; )</span><br></pre></td></tr></table></figure>

<h3 id="关于数组"><a href="#关于数组" class="headerlink" title="关于数组"></a>关于数组</h3><h4 id="数组相关运算符。"><a href="#数组相关运算符。" class="headerlink" title="数组相关运算符。"></a>数组相关运算符。</h4><table>
<thead>
<tr>
<th>运算符</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>$all</td>
<td>匹配包含查询中指定的所有元素的数组。（忽略顺序）</td>
</tr>
<tr>
<td>$size</td>
<td>如果数组字的大小为指定大小，则选择文档。</td>
</tr>
<tr>
<td>$elemMatch</td>
<td>如果array字段中的元素符合所有指定 $elemMatch 条件，则选择文档。</td>
</tr>
</tbody></table>
<p>下面的”tags”是一个数组</p>
<ol>
<li><p><strong>匹配一个数组</strong><br>这里匹配，必须是完全匹配，元素顺序也要相同。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123; tags: [<span class="string">&quot;red&quot;</span>, <span class="string">&quot;blank&quot;</span>] &#125; )</span><br></pre></td></tr></table></figure>
<p>相反，如果您希望找到一个同时包含元素”red”和 “blank”的数组，而不考虑该数组中的顺序或其他元素，请使用$all运算符：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123; tags: &#123; <span class="variable">$all</span>: [<span class="string">&quot;red&quot;</span>, <span class="string">&quot;blank&quot;</span>] &#125; &#125; )</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>针对一个元素去查询数组</strong><br>最简单的，数组包含这个元素：”tags”是一个数组，它有包含元素”red”</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123; tags: <span class="string">&quot;red&quot;</span> &#125; )</span><br></pre></td></tr></table></figure></li>
</ol>
<p>你还可以使用操作符指定查询条件：</p>
<p>dim_cm是一个数组，匹配条件是数组里面至少有一个大于25的元素。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123; dim_cm: &#123; <span class="variable">$gt</span>: 25 &#125; &#125; )</span><br></pre></td></tr></table></figure>



<ol start="3">
<li><strong>为数组元素指定多个条件</strong></li>
</ol>
<p>在数组元素上使用复合过滤条件查询数组:</p>
<p>注意匹配条件是，有一个元素大于15，有一个元素小于20（当然可以有同一个一个元素同时满足也算匹配）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123; dim_cm: &#123; <span class="variable">$gt</span>: 15, <span class="variable">$lt</span>: 20 &#125; &#125; )</span><br></pre></td></tr></table></figure>

<p>查询满足多个条件的数组元素：</p>
<p>注意必须是至少包含同一个元素同时满足大于22且小于30</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123; dim_cm: &#123; <span class="variable">$elemMatch</span>: &#123; <span class="variable">$gt</span>: 22, <span class="variable">$lt</span>: 30 &#125; &#125; &#125; )</span><br></pre></td></tr></table></figure>

<ol start="4">
<li><strong>通过数组索引位置查询元素</strong></li>
</ol>
<p>使用点号引用。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123; <span class="string">&quot;dim_cm.1&quot;</span>: &#123; <span class="variable">$gt</span>: 25 &#125; &#125; )</span><br></pre></td></tr></table></figure>



<p>按数组长度查询数组:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123; <span class="string">&quot;tags&quot;</span>: &#123; <span class="variable">$size</span>: 3 &#125; &#125; )</span><br></pre></td></tr></table></figure>


<h3 id="关于嵌入文档的数组"><a href="#关于嵌入文档的数组" class="headerlink" title="关于嵌入文档的数组"></a>关于嵌入文档的数组</h3><p>demo data</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.insertMany( [</span><br><span class="line">   &#123; item: <span class="string">&quot;journal&quot;</span>, instock: [ &#123; warehouse: <span class="string">&quot;A&quot;</span>, qty: 5 &#125;, &#123; warehouse: <span class="string">&quot;C&quot;</span>, qty: 15 &#125; ] &#125;,</span><br><span class="line">   &#123; item: <span class="string">&quot;notebook&quot;</span>, instock: [ &#123; warehouse: <span class="string">&quot;C&quot;</span>, qty: 5 &#125; ] &#125;,</span><br><span class="line">   &#123; item: <span class="string">&quot;paper&quot;</span>, instock: [ &#123; warehouse: <span class="string">&quot;A&quot;</span>, qty: 60 &#125;, &#123; warehouse: <span class="string">&quot;B&quot;</span>, qty: 15 &#125; ] &#125;,</span><br><span class="line">   &#123; item: <span class="string">&quot;planner&quot;</span>, instock: [ &#123; warehouse: <span class="string">&quot;A&quot;</span>, qty: 40 &#125;, &#123; warehouse: <span class="string">&quot;B&quot;</span>, qty: 5 &#125; ] &#125;,</span><br><span class="line">   &#123; item: <span class="string">&quot;postcard&quot;</span>, instock: [ &#123; warehouse: <span class="string">&quot;B&quot;</span>, qty: 15 &#125;, &#123; warehouse: <span class="string">&quot;C&quot;</span>, qty: 35 &#125; ] &#125;</span><br><span class="line">]);</span><br></pre></td></tr></table></figure>
<h4 id="查询嵌套在数组中的文档"><a href="#查询嵌套在数组中的文档" class="headerlink" title="查询嵌套在数组中的文档"></a>查询嵌套在数组中的文档</h4><p>相等匹配需要指定文档的精确匹配，包括字段顺序。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123; <span class="string">&quot;instock&quot;</span>: &#123; warehouse: <span class="string">&quot;A&quot;</span>, qty: 5 &#125; &#125; )</span><br></pre></td></tr></table></figure>

<h4 id="在文档数组中的字段上指定查询条件"><a href="#在文档数组中的字段上指定查询条件" class="headerlink" title="在文档数组中的字段上指定查询条件"></a>在文档数组中的字段上指定查询条件</h4><ol>
<li><p>对嵌入在文档数组中的字段指定查询条件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123; <span class="string">&#x27;instock.qty&#x27;</span>: &#123; <span class="variable">$lte</span>: 20 &#125; &#125; )</span><br></pre></td></tr></table></figure>
<p>其中instock是数组，qty是数组中的文档的字段。</p>
</li>
<li><p>使用数组索引查询嵌入文档中的字段</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123; <span class="string">&#x27;instock.0.qty&#x27;</span>: &#123; <span class="variable">$lte</span>: 20 &#125; &#125; )</span><br></pre></td></tr></table></figure></li>
</ol>
<h4 id="为文档数组指定多个条件"><a href="#为文档数组指定多个条件" class="headerlink" title="为文档数组指定多个条件"></a>为文档数组指定多个条件</h4><ol>
<li>同一个嵌入文档上指定多个条件</li>
</ol>
<p>使用<code>$elemMatch</code>运算符在一组嵌入文档上指定多个条件，以便至少一个嵌入文档满足所有指定的条件。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123; <span class="string">&quot;instock&quot;</span>: &#123; <span class="variable">$elemMatch</span>: &#123; qty: 5, warehouse: <span class="string">&quot;A&quot;</span> &#125; &#125; &#125; )</span><br></pre></td></tr></table></figure>
<p>意为：查询如下的文档，这些文档的 instock 字段（是一个文档数组）中，至少有一个嵌入文档，它的qty字段为5并且warehouse字段值为”A“。</p>
<ol start="2">
<li>多个嵌入文档元素组合满足标准</li>
</ol>
<p>如果数组字段上的复合查询条件不使用 $elemMatch运算符，则查询将选择其数组包含满足条件的任意元素组合的文档。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 不一定是同一个嵌入文档满足</span></span><br><span class="line">db.inventory.find( &#123; <span class="string">&quot;instock.qty&quot;</span>: &#123; <span class="variable">$gt</span>: 10,  <span class="variable">$lte</span>: 20 &#125; &#125; )</span><br><span class="line">db.inventory.find( &#123; <span class="string">&quot;instock.qty&quot;</span>: 5, <span class="string">&quot;instock.warehouse&quot;</span>: <span class="string">&quot;A&quot;</span> &#125; )</span><br></pre></td></tr></table></figure>


<h3 id="返回文档的部分字段"><a href="#返回文档的部分字段" class="headerlink" title="返回文档的部分字段"></a>返回文档的部分字段</h3><p>和SQL一样可以指定返回的字段。<br>默认情况下返回文档的全部内容。</p>
<p>语法：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.COLLECTION.find(过滤器, 投影文档)</span><br></pre></td></tr></table></figure>



<p>投影文档，形如：<br>表明只返回item、status状态</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123; item: 1, status: 1&#125;</span><br></pre></td></tr></table></figure>
<p>或者也可以反过来，指定不返回那些字段</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123; status: 0, instock: 0 &#125; </span><br></pre></td></tr></table></figure>

<p>支持字段的嵌套，如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123; item: 1, status: 1, <span class="string">&quot;size.uom&quot;</span>: 1 &#125;</span><br></pre></td></tr></table></figure>

<h3 id="查询空或缺失字段"><a href="#查询空或缺失字段" class="headerlink" title="查询空或缺失字段"></a>查询空或缺失字段</h3><p>demo data</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.insertMany([</span><br><span class="line">   &#123; _id: 1, item: null &#125;,</span><br><span class="line">   &#123; _id: 2 &#125;</span><br><span class="line">])</span><br></pre></td></tr></table></figure>

<h4 id="相等过滤器"><a href="#相等过滤器" class="headerlink" title="相等过滤器"></a>相等过滤器</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123; item: null &#125; )</span><br></pre></td></tr></table></figure>
<p>字段为null或者字段不存在都匹配</p>
<h4 id="类型检查"><a href="#类型检查" class="headerlink" title="类型检查"></a>类型检查</h4><p>BSON type编号为10 表明是null字段。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123; item : &#123; <span class="variable">$type</span>: 10 &#125; &#125; )</span><br></pre></td></tr></table></figure>
<p>只匹配null。</p>
<h4 id="存在性检查"><a href="#存在性检查" class="headerlink" title="存在性检查"></a>存在性检查</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.find( &#123; item : &#123; <span class="variable">$exists</span>: <span class="literal">false</span> &#125; &#125; )</span><br></pre></td></tr></table></figure>
<p>只匹配不存在。</p>
<h2 id="更新文档"><a href="#更新文档" class="headerlink" title="更新文档"></a>更新文档</h2><ol>
<li><p>更新单个文档</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.updateOne(</span><br><span class="line">   &#123; item: <span class="string">&quot;paper&quot;</span> &#125;,</span><br><span class="line">   &#123;</span><br><span class="line">     <span class="variable">$set</span>: &#123; <span class="string">&quot;size.uom&quot;</span>: <span class="string">&quot;cm&quot;</span>, status: <span class="string">&quot;P&quot;</span> &#125;</span><br><span class="line">   &#125;</span><br><span class="line">)</span><br></pre></td></tr></table></figure>
</li>
<li><p>替换整个文档</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.replaceOne(</span><br><span class="line">   &#123; item: <span class="string">&quot;paper&quot;</span> &#125;,</span><br><span class="line">   &#123; item: <span class="string">&quot;paper&quot;</span>, instock: [ &#123; warehouse: <span class="string">&quot;A&quot;</span>, qty: 60 &#125;, &#123; warehouse: <span class="string">&quot;B&quot;</span>, qty: 40 &#125; ] &#125;</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="更新方法"><a href="#更新方法" class="headerlink" title="更新方法"></a>更新方法</h3><p>MongoDB 提供了以下方法来更新集合中的文档：</p>
<table>
<thead>
<tr>
<th align="left">方法</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/method/db.collection.updateOne/#mongodb-method-db.collection.updateOne"><code>db.collection.updateOne()</code></a></td>
<td align="left">即使多个文档可能与指定的过滤器匹配，也最多更新与指定过滤器匹配的单个文档。<em>3.2版中的新功能</em>。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/method/db.collection.updateMany/#mongodb-method-db.collection.updateMany"><code>db.collection.updateMany()</code></a></td>
<td align="left">更新与指定过滤器匹配的所有文档。<em>3.2版中的新功能</em>。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/method/db.collection.replaceOne/#mongodb-method-db.collection.replaceOne"><code>db.collection.replaceOne()</code></a></td>
<td align="left">即使多个文档可能与指定的过滤器匹配，也最多替换与指定过滤器匹配的单个文档。<em>3.2版中的新功能</em>。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/method/db.collection.update/#mongodb-method-db.collection.update"><code>db.collection.update()</code></a></td>
<td align="left">更新或替换与指定过滤器匹配的单个文档，或者更新与指定过滤器匹配的所有文档。默认情况下，该<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/method/db.collection.update/#mongodb-method-db.collection.update"><code>db.collection.update()</code></a>方法更新<strong>单个</strong>文档。要更新多个文档，请使用<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/method/db.collection.update/#std-label-multi-parameter">multi</a>选项。</td>
</tr>
</tbody></table>
<h3 id="更新操作"><a href="#更新操作" class="headerlink" title="更新操作"></a>更新操作</h3><p>db.collection.updateOne(filter,update, options)<br>db.collection.updateMany(filter&gt;, update, options)<br>db.collection.replaceOne(filter, update, options)</p>
<p>第一个参数 filter 是过滤条件，也就是指定那些文档会被更新。</p>
<p>第二个参数 update指定具体的更新操作。</p>
<p>使用逗号分隔。</p>
<p>过滤器在介绍查询的时候已经介绍的差不多了。</p>
<p>update结构如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">   &lt;operator1&gt;: &#123; &lt;field1&gt;: &lt;value1&gt;, ... &#125;,</span><br><span class="line">   &lt;operator2&gt;: &#123; &lt;field2&gt;: &lt;value2&gt;, ... &#125;,</span><br><span class="line">   ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>其中 operator* 为操作符,下面会有介绍。</p>
<h3 id="更新操作符"><a href="#更新操作符" class="headerlink" title="更新操作符"></a>更新操作符</h3><p>这里只列出它们，详细的使用demo可以<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/">看这里</a>。</p>
<h3 id="字段"><a href="#字段" class="headerlink" title="字段"></a>字段</h3><table>
<thead>
<tr>
<th align="left">运算符</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/currentDate/#mongodb-update-up.-currentDate"><code>$currentDate</code></a></td>
<td align="left">将字段的值设置为当前日期，作为日期或时间戳。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/inc/#mongodb-update-up.-inc"><code>$inc</code></a></td>
<td align="left"><strong>将字段的值增加指定的数量。</strong></td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/min/#mongodb-update-up.-min"><code>$min</code></a></td>
<td align="left">仅当指定值小于现有字段值时才更新该字段。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/max/#mongodb-update-up.-max"><code>$max</code></a></td>
<td align="left">仅当指定值大于现有字段值时才更新该字段。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/mul/#mongodb-update-up.-mul"><code>$mul</code></a></td>
<td align="left">将字段的值乘以指定的数量。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/rename/#mongodb-update-up.-rename"><code>$rename</code></a></td>
<td align="left"><strong>重命名字段。</strong></td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/set/#mongodb-update-up.-set"><code>$set</code></a></td>
<td align="left"><strong>设置文档中字段的值。</strong></td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/setOnInsert/#mongodb-update-up.-setOnInsert"><code>$setOnInsert</code></a></td>
<td align="left">如果更新导致插入文档，则设置字段的值。对修改现有文档的更新操作没有影响。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/unset/#mongodb-update-up.-unset"><code>$unset</code></a></td>
<td align="left"><strong>从文档中删除指定的字段。</strong></td>
</tr>
</tbody></table>
<h3 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h3><h4 id="运算符-1"><a href="#运算符-1" class="headerlink" title="运算符"></a>运算符</h4><table>
<thead>
<tr>
<th align="left">运算符</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/positional/#mongodb-update-up.-"><code>$</code></a></td>
<td align="left">充当占位符以更新与查询条件匹配的第一个元素。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/positional-all/#mongodb-update-up.---"><code>$[]</code></a></td>
<td align="left">充当占位符，为匹配查询条件的文档更新数组中的所有元素。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/#mongodb-update-up.---identifier--"><code>$[]</code></a></td>
<td align="left">充当占位符，<code>arrayFilters</code>为符合查询条件的文档更新符合条件的所有元素。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/addToSet/#mongodb-update-up.-addToSet"><code>$addToSet</code></a></td>
<td align="left">仅当集合中尚不存在元素时，才将元素添加到数组中。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/pop/#mongodb-update-up.-pop"><code>$pop</code></a></td>
<td align="left"><strong>删除数组的第一项或最后一项。</strong></td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/pull/#mongodb-update-up.-pull"><code>$pull</code></a></td>
<td align="left">删除与指定查询匹配的所有数组元素。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/push/#mongodb-update-up.-push"><code>$push</code></a></td>
<td align="left"><strong>将项目添加到数组。</strong></td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/pullAll/#mongodb-update-up.-pullAll"><code>$pullAll</code></a></td>
<td align="left">从数组中删除所有匹配的值。</td>
</tr>
</tbody></table>
<h4 id="修饰符"><a href="#修饰符" class="headerlink" title="修饰符"></a>修饰符</h4><table>
<thead>
<tr>
<th align="left">运算符</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/each/#mongodb-update-up.-each"><code>$each</code></a></td>
<td align="left">修改<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/push/#mongodb-update-up.-push"><code>$push</code></a>和<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/addToSet/#mongodb-update-up.-addToSet"><code>$addToSet</code></a>运算符以附加多个项目以进行数组更新。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/position/#mongodb-update-up.-position"><code>$position</code></a></td>
<td align="left">修改<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/push/#mongodb-update-up.-push"><code>$push</code></a>运算符以指定要在数组中添加元素的位置。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/slice/#mongodb-update-up.-slice"><code>$slice</code></a></td>
<td align="left">修改<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/push/#mongodb-update-up.-push"><code>$push</code></a>运算符以限制更新数组的大小。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/sort/#mongodb-update-up.-sort"><code>$sort</code></a></td>
<td align="left">修改<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/push/#mongodb-update-up.-push"><code>$push</code></a>运算符以重新排序存储在数组中的文档。</td>
</tr>
</tbody></table>
<h3 id="按位"><a href="#按位" class="headerlink" title="按位"></a>按位</h3><table>
<thead>
<tr>
<th align="left">姓名</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/update/bit/#mongodb-update-up.-bit"><code>$bit</code></a></td>
<td align="left">执行整数值的按位<code>AND</code>、<code>OR</code>和<code>XOR</code>更新。</td>
</tr>
</tbody></table>
<h3 id="行为"><a href="#行为" class="headerlink" title="行为"></a>行为</h3><ul>
<li><p>原子性<br><strong>MongoDB 中的所有写操作在单个文档的级别上都是原子的</strong>。</p>
</li>
<li><p>_id字段<br>设置后，您不能更新_id字段的值，也不能用具有不同_id字段值的替换文档替换现有文档。</p>
</li>
<li><p>次序</p>
</li>
</ul>
<p>  对于写操作，MongoDB 会保留文档字段的顺序， 但以下情况除外：</p>
<ul>
<li><code>_id</code>字段始终是文档中的第一个字段。</li>
<li>包含renaming字段名称的更新可能会导致文档中字段的重新排序。</li>
</ul>
<h2 id="删除文档"><a href="#删除文档" class="headerlink" title="删除文档"></a>删除文档</h2><p>删除操作只需传入一个<strong>过滤器</strong>指定那些文档需要被删除即可。</p>
<p>举例：</p>
<ul>
<li>删除所有文档<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.deleteMany(&#123;&#125;)</span><br></pre></td></tr></table></figure></li>
<li>删除符合条件的文档<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.collection.deleteMany(&lt;filter&gt;)</span><br></pre></td></tr></table></figure></li>
<li>只删除一个符合条件的文档<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.collection.deleteMany(&lt;filter&gt;)</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="删除方法"><a href="#删除方法" class="headerlink" title="删除方法"></a>删除方法</h3><table>
<thead>
<tr>
<th align="left">方法</th>
<th align="left"></th>
</tr>
</thead>
<tbody><tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/method/db.collection.deleteOne/#mongodb-method-db.collection.deleteOne"><code>db.collection.deleteOne()</code></a></td>
<td align="left">即使多个文档可能与指定的过滤器匹配，也最多删除一个与指定过滤器匹配的文档。<em>3.2版中的新功能</em>。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/method/db.collection.deleteMany/#mongodb-method-db.collection.deleteMany"><code>db.collection.deleteMany()</code></a></td>
<td align="left">删除与指定过滤器匹配的所有文档。<em>3.2版中的新功能</em>。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/method/db.collection.remove/#mongodb-method-db.collection.remove"><code>db.collection.remove()</code></a></td>
<td align="left">删除单个文档或与指定过滤器匹配的所有文档。</td>
</tr>
</tbody></table>
<h3 id="行为-1"><a href="#行为-1" class="headerlink" title="行为"></a>行为</h3><ul>
<li>索引</li>
</ul>
<p><strong>删除操作不会删除索引</strong>，即使从集合中删除所有文档也是如此。</p>
<ul>
<li>原子性</li>
</ul>
<p>MongoDB 中的所有写操作在单个文档的级别上都是原子的。</p>
<blockquote>
<p>参考文献<br><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/sql-comparison/">SQL到MongoDB的转换</a></p>
<p><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/crud/">MongoDB的CRUD</a></p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/NoSQL/" rel="tag"># NoSQL</a>
              <a href="/tags/MongoDB/" rel="tag"># MongoDB</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/10/04/MongoDB%E5%85%A5%E9%97%A8/" rel="prev" title="M1 MacOS 下使用Brew安装MongoDB、入门概念">
                  <i class="fa fa-chevron-left"></i> M1 MacOS 下使用Brew安装MongoDB、入门概念
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/10/09/Ubuntu%E3%80%81MacOS%E7%8E%AF%E5%A2%83%E4%B8%8B%E4%B8%8B%E8%BD%BD%E5%AE%89%E8%A3%85%20RabbitMQ/" rel="next" title="Ubuntu、MacOS环境下下载安装 RabbitMQ">
                  Ubuntu、MacOS环境下下载安装 RabbitMQ <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">SongyangJi</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  





</body>
</html>
